<template>
  <div :class="className" :style="{height:height,width:width}"></div>
</template>

<script>
  import echarts from 'echarts'

  require('echarts/theme/macarons') // echarts theme
  import {debounce} from '@/utils'

  const animationDuration = 1000

  export default {
    name: 'refer-time-chart',
    props: {
      className: {
        type: String,
        default: 'chart'
      },
      width: {
        type: String,
        default: '100%'
      },
      height: {
        type: String,
        default: '300px'
      },
      xData: Array,
      yData: Array,
    },
    data() {
      return {
        chart: null
      }
    },
    watch: {
      yData() {
        this.renderChart();
      }
    },
    mounted() {
      this.initChart()
      this.__resizeHanlder = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHanlder)
    },
    beforeDestroy() {
      if (!this.chart) {
        return
      }
      window.removeEventListener('resize', this.__resizeHanlder)
      this.chart.dispose()
      this.chart = null
    },
    methods: {
      initChart() {
        this.chart = echarts.init(this.$el, 'macarons')
        this.renderChart()
      },
      renderChart() {
        const series = this.yData;
        this.chart.setOption({
          tooltip : {
            trigger: 'axis'
          },
          legend: {
            data:['baidu','google','google','google','google'],
            // data:_(consts.PLATFORM_TYPE).filter(info => (info.value !== consts.ALL && info.value != consts.YB)).map('title').value(),
            align: 'auto',
            right: '10%'
          },
          // dataZoom: {
          //   show: true,
          //   realtime: true,
          //   start: 0,
          //   end: 100
          // },

          calculable : true,
          xAxis : [
            {
              type : 'category',
              boundaryGap : false,
              data : this.xData
            }
          ],
          yAxis : [
            {
              type : 'value',
              show: false,
            }
          ],
          series: series

        })

      }
    }
  }
</script>
